﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;

namespace PBDesk.Utilities
{
    public static class Extensions
    {
        public static T ToEnum<T>(this int input, bool ignoreCase = true, bool throwException = true) where T : struct
        {
            return (T)MyUtils.ParseEnum<T>(input, throwException);
        }
        public static T ToEnum<T>(this int input, T defaultValue, bool ignoreCase = true, bool throwException = false) where T : struct
        {
            return (T)MyUtils.ParseEnum<T>(input, defaultValue, throwException);
        }

        public static string ToQueryString(this NameValueCollection nvc)
        {
            return "?" + string.Join("&", Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(nvc[key]))));
        }

        public static string ToDelimitedString(this NameValueCollection nvc, string pairDelimiter, string keyValueDelimiter)
        {
            return string.Join(pairDelimiter, Array.ConvertAll(nvc.AllKeys, key => string.Format("{0}{1}{2}", key,keyValueDelimiter, nvc[key])));
        }
    }
}
